<?php
namespace app\web\controller;
use app\web\model\LineEmployeeModel;
use app\web\model\LineZhiweiModel;
use app\web\model\ChainStoreModel;
use app\admin\model\StoreMenu as StoreMenuModel;
use app\common\controller\StoreBaseController;
use think\Db;
/**
 * 店铺员工列表
 */
class EmployeeController extends StoreBaseController
{
	protected $model;
	protected $zwmodel;
	protected $menumodel;
	protected $chainmodel;
	
    protected function _initialize() {
        parent::_initialize();
		$this->model = new LineEmployeeModel();
		$this->zwmodel = new LineZhiweiModel();
		$this->menumodel = new StoreMenuModel();
		$this->chainmodel = new ChainStoreModel();
		$count=Db::name('line_employee')->where('agent_id',session('storeInfo')['id'])->count();
 		 $this->assign('count',$count);
 		 $zhiwei=db('line_zhiwei')->where('agent_id',session('storeInfo')['id'])->select();
 		 $this->assign('zhiwei',$zhiwei);
    }
	
	
	public function index(){
		$where['agent_id']= session('storeInfo')['id'];
		$list = db("line_employee")->alias('e')->field('e.*,z.name as zwname,c.name as lsname,a.store_name as sjname')
		->join('mall_agent a','e.agent_id=a.id','LEFT')
		->join('mall_chainstore c','e.chainstore_id=c.id','LEFT')
		->join('line_zhiwei z','e.z_id=z.id','LEFT')->where('e.agent_id',$where['agent_id'])->select();		
		
		return $this->fetch('index', ['list' => $list]);
	}
    /**
     * 
     *添加
     * @author 
     */
     public function add()
    {
    	$storeinfo = session('storeInfo');
        if(request()->isAjax()){
        	$param=input('post.');
			$param['add_time'] = now_time();
			$param['agent_id'] = session('storeInfo')['id'];
			$res=db('line_employee')->insert($param);
			if ($res) {
                $this->success('添加成功', url('index'));
            } else {
                $this->error('添加失败');
      		}
       }
		$zhiwei=db('line_zhiwei')->where('agent_id',$storeinfo['id'])->select();
		$storelist = db("mall_agent")->alias('a')->field('c.id,c.name')
		->join('mall_chainstore c','a.u_id=c.u_id','LEFT')->where('agent_id',$storeinfo['id'])->order('c.sort desc')->select();			
		$this->assign('zhiwei',$zhiwei);
		$this->assign('storelist',$storelist);
		$this->assign('storeid',$storeinfo['id']);
		$this->assign('storename',$storeinfo['store_name']);
        return $this->fetch();
  
   }
    /**
     * 
     *编辑
     * @author 
     */
    public function edit($id)
    {
    	$storeinfo = session('storeInfo');
        if(request()->isPost()){
            $param = input('post.');
            $param['chainstore_id']=$param['agent_id'];
            $param['agent_id']=session('storeInfo')['id'];
         	if ($this->model->allowField(true)->save($param,$id) !== false) {
                $this->success('修改成功', url('employee/index'));
            } else {
                $this->error('修改失败');
            }
        }
		//列出职位
		$zhiwei=db('line_zhiwei')->where('agent_id',$storeinfo['id'])->select();
		$storelist = db("mall_agent")->alias('a')->field('c.chainstore_id,c.id,c.name')
		->join('mall_chainstore c','a.u_id=c.u_id','LEFT')->where('agent_id',$storeinfo['id'])->order('c.sort desc')->select();		
        $id = input('param.id');
        $this->assign('list',$this->model->where('agent_id',session('storeInfo')['id'])->find($id));
		$this->assign('zhiwei',$zhiwei);
		
		$this->assign('storelist',$storelist);
		$this->assign('storeid',$storeinfo['id']);
		$this->assign('storename',$storeinfo['store_name']);
        return $this->fetch();
        
    }

	 /**
     * [del删除]
     * @author [longx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
	
	public function zhiwei( $page = 1){
		$where['agent_id']= session('storeInfo')['id'];
		$list = db("line_zhiwei")->where($where)->select();
		return $this->fetch('zhiwei', ['list' => $list]);
	}
	/**
	 * 新增职位
	 */
	public function addzw(){
		$data['agent_id']=session('storeInfo')['id'];
		$data['name']=input("name");
		$data['add_time']=now_time();
		$res=db('line_zhiwei')->insert($data);
		if($res){
			return json(['code'=>200,'msg'=>"保存成功"]);
		}else{
			return json(['code'=>408,'msg'=>"保存失败"]);
		}
	}
	 /**
     * [del删除]
     * @author [longx] 
     */
    public function deletezw()
    {
        $id = input('param.id');
        if ($this->zwmodel->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
	
    /**
     * [edit 编辑]
     * @return [type] [description]
     * @author []
     */
    public function editzw()
    {
    	$data['agent_id']=session('storeInfo')['id'];
		$data['name']=input('name');
		$data['id']=input('id');
		$res=db('line_zhiwei')->update($data);
		if($res){
			return json(['code'=>200,'msg'=>"修改成功"]);
		}else{
			return json(['code'=>408,'msg'=>"修改失败"]);
		}
          	
    }
	
	/**
     * 授权
     * @param $id
     * @return mixed
     */
    public function auth($id, $auth_rule_ids = '') {
    	if ($this->request->isPost()) {
            if ($id) {
                $group_data['id']    = $id;
                $group_data['rules'] = is_array($auth_rule_ids) ? implode(',', $auth_rule_ids) : '';
                if ($this->zwmodel->save($group_data, $id) !== false) {
                    $this->success('授权成功', url('zhiwei'));
                } else {
                    $this->error('授权失败');
                }
            }
        }else{
        	return $this->fetch('auth', ['id' => $id]);
        }
    }

    /**
     * AJAX获取规则数据
     * @param $id
     * @return mixed
     */
    public function getJson($id) {
        $auth_group_data = $this->zwmodel->find($id)->toArray();
        $auth_rules      = explode(',', $auth_group_data['rules']);
        $auth_rule_list  = $this->menumodel->field('id,pid,title')->select();

        foreach ($auth_rule_list as $key => $value) {
            in_array($value['id'], $auth_rules) && $auth_rule_list[$key]['checked'] = true;
        }

        return $auth_rule_list;
    }
}